<template>
  <transition name="el-fade-in">
    <div class="mask" v-show="$store.state.show_recharge" @click="close">
      <div @click.stop>
        <!-- <div class="left">
          <span v-if="$store.state.order_type == 'vip'">购买年费会员</span>
          <span v-if="$store.state.order_type == 'lesson'">购买本系列课程</span>
          <img :src="$store.state.company_logo" alt />
          <div>
            <div>
              <p>时间</p>
              <span v-if="$store.state.order_type == 'vip'">一年</span>
              <span v-if="$store.state.order_type == 'lesson'">永久</span>
            </div>
            <div>
              <p>价格</p>
              <span v-if="$store.state.order_type == 'vip'">￥{{ year_price }}</span>
              <span v-if="$store.state.order_type == 'lesson'">￥{{ $store.state.group_price }}</span>
            </div>
          </div>
        </div>-->
        <div class="right">
          <div class="title">支付方式</div>
          <div class="btn-list">
            <div
              v-for="(item,index) in type_list"
              :key="index"
              :style="(type_index == index ? 'border-color: #ffe300;' : '') + ';background:' + item.color"
              @click="type_index = index"
            >
              <img :src="item.icon" alt />
              <span>{{item.name}}</span>
            </div>
          </div>
          <div class="title" v-show="$store.state.order_type == 'vip'">会员套餐</div>
          <div class="time-list" v-show="$store.state.order_type == 'vip'">
            <div
              v-for="(item,index) in time_list"
              :key="index"
              :style="time_index == index ? 'border-color: #ffe300;background: rgba(255, 227, 0, 0.1)' : ''"
              @click="time_index = index"
            >
              <span>{{item.name}}</span>
              <b>￥{{item.price}}</b>
            </div>
          </div>
          <div class="title" v-if="coupons_list.length">优惠券</div>
          <div class="time-list">
            <div
              v-for="(item,index) in coupons_list"
              :key="index"
              :style="coupons_index == index ? 'border-color: #ffe300;background: rgba(255, 227, 0, 0.1)' : ''"
              @click="coupons_index = index"
            >
              <span>{{item.coupons.name}}</span>
              <b v-show="item.coupons.price">￥{{item.coupons.price}}</b>
              <p v-show="item.coupons.start">有效期：{{item.coupons.start}}至{{item.coupons.end}}</p>
            </div>
          </div>
          <p>购买后可享受所有课程观看</p>
          <div class="btn" @click="recharge()">去支付</div>
          <transition name="el-fade-in">
            <div class="qr-code" v-show="show_qr" @click="show_qr = false">
              <div id="recharge"></div>
            </div>
          </transition>
          <!-- <p>会员类型：年费会员</p> -->
          <!-- <p>有效期：2019-10-10</p> -->
          <!-- <p>{{ text }}</p> -->
          <!-- <transition name="el-fade-in">
            <div class="qr-code" v-show="is_success">
              <div class="success">
                <i class="el-icon-success"></i>
                <span>支付成功，3秒后刷新页面</span>
              </div>
            </div>
          </transition>-->
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
import QRCode from "qrcodejs2";
let code;
let status;
export default {
  data() {
    return {
      logo: localStorage.getItem("company_logo"),
      text: "",
      show_qr: false,
      is_success: false,
      type_list: [
        {
          icon: require("@/assets/zfb.png"),
          name: "支付宝",
          type: 1,
          color: "#108ee9"
        },
        {
          icon: require("@/assets/wx.png"),
          name: "微信",
          type: 2,
          color: "#00d500"
        }
      ],
      type_index: 0,
      time_list: [
        {
          name: "连续包月",
          type: 1
        },
        {
          name: "连续包季",
          type: 2
        },
        {
          name: "连续包年",
          type: 3
        },
        {
          name: "年费会员",
          type: 4
        }
      ],
      time_index: 0,
      coupons_list: [],
      coupons_index: 0
    };
  },
  created() {
    let _this = this;
    _this.$axios.get("site/vip").then(res => {
      _this.time_list[0].price = res.month;
      _this.time_list[1].price = res.quarter;
      _this.time_list[2].price = res.annual;
      _this.time_list[3].price = res.year;
    });
    if (_this.$store.state.is_login) {
      _this.$axios.get("user/coupons").then(res => {
        _this.coupons_list = res;
        _this.coupons_list.push({
          coupons: {
            name: "不使用优惠券"
          }
        });
      });
    }
  },
  methods: {
    close() {
      this.$store.state.show_recharge = false;
      this.show_qr = false;
    },
    recharge() {
      let _this = this;
      if (_this.$store.state.is_login) {
        if (status) {
          window.clearInterval(status);
        }
        let url =
          _this.$store.state.order_type == "vip" ? "user/buy" : "group/buy";
        let options = {
          pay_type: _this.type_list[_this.type_index].type,
          vip_type: _this.time_list[_this.time_index].type,
          lesson_group_id: _this.$store.state.group_id
        };
        console.log(_this.coupons_list[_this.coupons_index].id);
        if (_this.coupons_list[_this.coupons_index].id) {
          options.user_coupons_id = _this.coupons_list[_this.coupons_index].id;
        }
        _this.$axios.post(url, options).then(res => {
          if (res) {
            if (_this.type_index == 0) {
              const div = document.createElement("div");
              div.innerHTML = res;
              document.body.appendChild(div);
              document.forms[0].submit();
            } else if (_this.type_index == 1) {
              _this.show_qr = true;
              if (code) {
                code.makeCode(res.qr);
              } else {
                code = new QRCode(document.getElementById("recharge"), {
                  width: 200,
                  height: 200,
                  text: res.qr
                });
              }
              _this.getStatus(res.id);
            }
          }
        });
      } else {
        _this.$store.state.show_login = true;
      }
    },
    getStatus(id) {
      let _this = this;
      status = setInterval(() => {
        _this.$axios
          .get("pay/check", {
            params: {
              id: id
            }
          })
          .then(res => {
            _this.text = res.comment;
            if (res.status == 1) {
              window.clearInterval(status);
              _this.$router.go(0);
              localStorage.setItem("user_vip", 1);
            }
          });
      }, 1000);
    }
  }
};
</script>

<style lang="less" scoped>
.mask {
  width: 100%;
  height: 100vh;
  background: rgba(0, 0, 0, 0.5);
  position: fixed;
  left: 0;
  top: 0;
  z-index: 2002;
  > div {
    background: rgb(0, 46, 67);
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    display: flex;
    flex-direction: row;
    align-items: stretch;
    box-shadow: 0 0 0.3rem 0 black;
    @media only screen and (max-width: 500px),
      only screen and (max-device-width: 500px) {
      width: calc(100% - 0.6rem);
    }
    .left {
      width: 40%;
      background: black;
      // position: absolute;
      // left: 0;
      // top: 50%;
      // transform: translateY(-50%);
      box-sizing: border-box;
      padding: 0.3rem;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: space-between;
      color: @gold-color;
      > span {
        font-size: @big-size;
        font-weight: bold;
      }
      > img {
        width: 100%;
        height: 2rem;
        object-fit: contain;
      }
      > div {
        width: 100%;
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        > div {
          display: flex;
          flex-direction: column;
          align-items: center;
          > span {
            font-size: @title-size;
          }
        }
      }
    }
    .right {
      background: white;
      display: flex;
      align-items: center;
      flex-direction: column;
      box-sizing: border-box;
      padding: 0.3rem;
      padding-right: 0;
      color: black;
      @media only screen and (max-width: 500px),
        only screen and (max-device-width: 500px) {
        width: 100%;
      }
      .title {
        font-size: @title-size;
        font-weight: bold;
        margin-bottom: 0.2rem;
        margin-right: 0.3rem;
      }
      .btn-list {
        display: flex;
        flex-direction: row;
        align-items: center;
        flex-wrap: wrap;
        > div {
          width: 1.8rem;
          height: 0.6rem;
          display: flex;
          flex-direction: row;
          align-items: center;
          justify-content: center;
          border-radius: 0.05rem;
          cursor: pointer;
          transition: 300ms;
          box-sizing: border-box;
          border: 2px white solid;
          color: white;
          margin-bottom: 0.2rem;
          margin-right: 0.3rem;
          > img {
            width: 0.3rem;
            height: 0.3rem;
            margin-right: 0.05rem;
          }
        }
        > div:hover {
          border: 2px @gold-color solid;
        }
      }
      .time-list {
        max-width: @width;
        display: flex;
        flex-direction: row;
        align-items: center;
        flex-wrap: wrap;
        @media only screen and (max-width: 500px),
          only screen and (max-device-width: 500px) {
          width: 100%;
          justify-content: center;
        }
        > div {
          padding: 0.1rem 0.3rem;
          display: flex;
          flex-direction: column;
          align-items: center;
          justify-content: center;
          border-radius: 0.05rem;
          cursor: pointer;
          transition: 300ms;
          box-sizing: border-box;
          border: 2px white solid;
          color: @black-color;
          font-weight: bold;
          border: 2px #f1f1f1 solid;
          font-size: @normal-size;
          margin-bottom: 0.2rem;
          margin-right: 0.3rem;
          > b {
            font-size: @title-size;
            color: #f56c6c;
            margin-right: 0.1rem;
          }
          > p {
            font-size: @small-size;
            margin-right: 0.1rem;
            color: @dark-color;
            font-weight: normal;
          }
        }
        > div:hover {
          border: 2px @gold-color solid;
          background: rgba(@gold-color, 0.2);
        }
      }
      .success {
        margin-top: 0.2rem;
        margin-right: 0.3rem;
        display: flex;
        flex-direction: column;
        align-items: center;
        > i {
          font-size: 0.5rem;
          color: #00d500;
        }
        > span {
          margin-top: 0.2rem;
        }
      }
      .btn {
        width: 1.2rem;
        height: 0.5rem;
        margin-top: 0.2rem;
        margin-right: 0.3rem;
        font-size: @title-size;
        display: flex;
        align-items: center;
        justify-content: center;
        color: white;
        background: black;
        box-shadow: 0 0 0.1rem 0 black;
        cursor: pointer;
        transition: 300ms;
        border-radius: 1rem;
      }
      .btn:hover {
        background: gold;
        font-weight: bold;
        color: black;
      }
      > i {
        font-size: 1rem;
        color: #00d500;
      }
      > p {
        margin-right: 0.3rem;
        font-size: @small-size;
        margin-bottom: 0.1rem;
      }
      .qr-code {
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.5) !important;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        background: white;
        display: flex;
        align-items: center;
        justify-content: center;
        > div {
          padding: 0.1rem;
          background: white;
          border: 2px @gold-color solid;
        }
      }
    }
  }
}
</style>
